
$(function() {	
	Tip.emp('报销人');
	
	$("input[id^='amount']:visible").css("width", 85);
});

function expenselog(id) {
	var url = 'finance.php?do=ExpenseLog.view&id=' + id;
	
	_dialog_view(url, '查看信息', '550x350');
}

var ExpenseLog = {
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'finance.php?do=ExpenseLog.edit&id=' + id;
		var c2 = 'finance.php?do=ExpenseLog.view&id=' + id;
				
		var a1 = ['编辑信息', 'url', 'ExpenseLog.dialog_edit_', '600x380'];	
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x380'];
		
		var s = c1 + ", " + c2;

		return popup(e, [a1, a2], s);
	},

	dialog_edit_: function (url, title, xy) {
		result = Common.result();
		$.getJSON('finance.php?do=Expense.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
			
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
			
						var form = $("#form_edit")[0];
						
						var body = { };
						body.id = form.id.value;
						body.expense = form.expense.value;
						body.date = form.date.value;
						body.type = form.type.value;
						body.name = form.name.value;						
						body.amount = form.amount.value;
						
						$(this).dialog("close");
			
						var url = 'finance.php?do=ExpenseLog.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
			
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
			
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'finance.php?do=ExpenseLog.delete' + p2;
		var c2 = 'finance.php?do=ExpenseLog.batch_type';
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		var a2 = ['更新类型', 'url', 'ExpenseLog.dialog_batch_type', '800x450'];

		var s = c1 + ", " + c2;

		return popup(e, [a1, a2], s);
	},
		
	dialog_batch_type: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('finance.php?do=ExpenseLog.audit&item=151', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
						
						var cc = ExpenseLog._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.type = form.type.value;
						body.content = form.content.value;
						
						var url = 'finance.php?do=ExpenseLog.batch_typePost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},
	
	_cc: function () {
		var c = new Array();
		$("input[name^='b_']").each(function() {
			if ($(this).attr("checked")) {
				c.push($(this).val());
			}
		});
		
		return c;
	}
};
